package org.zsqx.action;

import java.sql.SQLException;
import java.util.Calendar;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.zsqx.bean.Dstype;
import org.zsqx.bean.SumReports;
import org.zsqx.bean.Town;
import org.zsqx.dao.ReportsDAOImpl;
import org.zsqx.tools.ReportTools;

@Action(value="/sumReportsAction",results={
		@Result(name="reports_town",location="/login/disasterReports.jsp"),
		@Result(name="reports_year",location="/login/disasterReports.jsp"),
		@Result(name="reports_month",location="/login/disasterReports.jsp"),
		@Result(name="reports_dsType",location="/login/disasterReports.jsp"),
		@Result(name="reports_gjIndustry",location="/login/disasterReports.jsp"),
		@Result(name="reports_nymIndustry",location="/login/disasterReports.jsp")
})

public class SumReportsAction extends BaseAction {
	private ReportsDAOImpl reportDao = new ReportsDAOImpl();
	private SumReports report;
	private String disType;
	private List<SumReports> reportList;
	private String report_type;
	private List<Town> towns;
	private List<Dstype> dstypes;
	private String year;
	private String month;
	private String reportTitle;
	private String message;
	
	public String reports_town() throws SecurityException, SQLException, InstantiationException, IllegalAccessException, NoSuchFieldException{
		report_type="reports_town";
		dstypes = rd.find(new Dstype());
		if(null==report){
			report=new SumReports();
		}
		report.setYear(year);
		report.setMonth(month);
		reportTitle = ReportTools.getReportTownTitle(report);
		reportList = reportDao.getTownReport(report);
		return "reports_town";
	}
	public String reports_year() throws SecurityException, SQLException, InstantiationException, IllegalAccessException, NoSuchFieldException{
		report_type="reports_year";
		dstypes = rd.find(new Dstype());
		if(null==report){
			report=new SumReports();
			report.setStartYear((Calendar.getInstance().get(Calendar.YEAR)-1)+"");
			report.setEndYear(Calendar.getInstance().get(Calendar.YEAR)+"");
		}
		reportTitle = ReportTools.getReportTownTitle(report);
		reportList = reportDao.getYearReport(report);
		return "reports_year";
	}
	public String reports_month() throws SecurityException, SQLException, InstantiationException, IllegalAccessException, NoSuchFieldException{
		report_type="reports_month";
		towns = rd.find(new Town());
		if(null==report){
			report=new SumReports();
		}
		report.setYear(year);
		report.setMonth(month);
		reportTitle = ReportTools.getReportMonthTitle(report);
		reportList = reportDao.getMonthReport(report);
		return "reports_town";
	}
	public String reports_dsType(){
		report_type="reports_dsType";
		return "reports_dsType";
	}
	public String reports_gjIndustry() throws SecurityException, SQLException, InstantiationException, IllegalAccessException, NoSuchFieldException{
		report_type="reports_gjIndustry";
		dstypes = rd.find(new Dstype());
		report = new SumReports();
		report.setDstype(disType);
		report.setYear(year);
		reportList = reportDao.getGjReport(report);
		return "reports_gjIndustry";
	}
	public String reports_nymIndustry() throws SecurityException, SQLException, InstantiationException, IllegalAccessException, NoSuchFieldException{
		report_type="reports_nymIndustry";
		dstypes = rd.find(new Dstype());
		report = new SumReports();
		report.setDstype(disType);
		report.setYear(year);
		reportList = reportDao.getNymReport(report);
		return "reports_nymIndustry";
	}
	public String getReport_type() {
		return report_type;
	}
	public void setReport_type(String reportType) {
		report_type = reportType;
	}
	public List<SumReports> getReportList() {
		return reportList;
	}
	public void setReportList(List<SumReports> reportList) {
		this.reportList = reportList;
	}
	public List<Town> getTowns() {
		return towns;
	}
	public void setTowns(List<Town> towns) {
		this.towns = towns;
	}
	public List<Dstype> getDstypes() {
		return dstypes;
	}
	public void setDstypes(List<Dstype> dstypes) {
		this.dstypes = dstypes;
	}
	public String getYear() {
		return year;
	}
	public void setYear(String year) {
		this.year = year;
	}
	public SumReports getReport() {
		return report;
	}
	public void setReport(SumReports report) {
		this.report = report;
	}
	public String getReportTitle() {
		return reportTitle;
	}
	public void setReportTitle(String reportTitle) {
		this.reportTitle = reportTitle;
	}
	public String getMonth() {
		return month;
	}
	public void setMonth(String month) {
		this.month = month;
	}
	public String getDisType() {
		return disType;
	}
	public void setDisType(String disType) {
		this.disType = disType;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	@Override
	public void validate() {
		if(null!=report&&null!=report.getDstype()&&report.getDstype().equals("0")){
			report.setDstype(null);
		}
		if(null!=report&&null!=report.getTown()&&report.getTown().equals("0")){
			report.setTown(null);
		}
		if(null!=year&&year.equals("0")){
			year=null;
		}
		if(null==year){
			year=Calendar.getInstance().get(Calendar.YEAR)+"";
		}
		if(null!=month&&month.equals("0")){
			month=null;
		}
		if(null!=disType&&disType.equals("0")){
			disType=null;
		}
		if(null!=report&&report.getStartYear()!=null){
			if(report.getStartYear().equals("0")){
				report.setStartYear(null);
			}
			if(report.getEndYear().equals("0")){
				report.setEndYear(null);
			}
		}
		super.validate();
	}
}
